package com.yxk;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringCloudApplication
public class UserServer4 {
    public static void main(String[] args) {
        SpringApplication.run(UserServer4.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    class DcController {

        @Autowired
        ConsumerService consumerService;

        @GetMapping("/consumer")
        public String dc() {
            return "user-server4" + consumerService.consumer();
        }

    }

    @Service
    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer() {
            return restTemplate.getForObject("http://product-server/dc", String.class);
        }

        public String fallback() {
            return "fallback";
        }

        public String fallback(int i) {
            return "i1323";
        }

    }
}
